package orbito.physics;

import orbito.physics.motion.Actor;

public class PhysicsUtils {
	
	/**
	 * Determines if a child actor is in a state of orbit around a parent actor, given their 
	 * current physical states, assuming ballistic trajectories and gravitation.
	 * 
	 * Note that this method only takes into account the given actors, thus, other actors may 
	 * influence the child actor to "turn around" and altogether join orbit once more.
	 * 
	 * @param child  The orbiting actor.
	 * @param parent The parent actor.
	 * @return       true if child is in orbit around its parent, false otherwise.
	 */
	public static boolean isInOrbit(Actor child, Actor parent) {
		double relativeSpeed = parent.getVelocity().sub(child.getVelocity()).length();
		double relativeDistance = parent.getPosition().sub(child.getPosition()).length();
		double escapeSpeed = Math.sqrt(2 * PhysicsSettings.gravitationalCoefficient * parent.getMass() / relativeDistance);
		return relativeSpeed < escapeSpeed;
	}
}
